<?php

declare(strict_types=1);

namespace App\CustHub\Service;

use App\CustHub\Mapper\ChApiDocMapper;
use App\System\Mapper\SystemApiGroupMapper;
use App\System\Mapper\SystemApiMapper;
use App\System\Mapper\SystemAppGroupMapper;
use App\System\Mapper\SystemAppMapper;
use App\System\Model\SystemApp;
use App\System\Service\SystemAppService;
use Hyperf\Di\Annotation\Inject;
use Mine\Abstracts\AbstractService;
use Mine\Annotation\Transaction;
use Mine\Exception\NormalStatusException;

class AppService extends AbstractService
{
    #[Inject]
    protected SystemAppService $appService;

    #[Inject]
    protected SystemAppMapper $appMapper;

    #[Inject]
    protected SystemAppGroupMapper $appGroupMapper;

    #[Inject]
    protected SystemApiMapper $apiMapper;

    #[Inject]
    protected SystemApiGroupMapper $apiGroupMapper;

    #[Inject]
    protected ChApiDocMapper $chApiDocMapper;


    #[Transaction]
    public function bindUser(mixed $uid,string $nickname)
    {
        $info = $this->appService->getAppInfoByUid($uid);
        if(empty($info)){
            //获取应用分组
            $group = $this->appGroupMapper->getGroupByCode('wework');
            $groupId = $group ? $group->id : 0;

            $data = [
                'uid' => $uid,
                'group_id' => $groupId,
                'app_name' => $nickname,
                'app_id' => $this->appService->getAppId(),
                'app_secret' => $this->appService->getAppSecret()
            ];
            //新应用信息
            $appId = $this->appService->save($data);
        }else{
            $appId = $info['id'];
        }

        //获取接口分组信息
        $apiGroup = $this->apiGroupMapper->getGroupByCode('wework');
        $groupId = $apiGroup ? $apiGroup->id : 0;
        if(!empty($groupId)){
            $apiIds = $this->apiMapper->getIdsByGroupId($groupId);
            $this->appService->bind($appId,$apiIds);
        }
        $this->appMapper->clearCache($uid);
    }

    public function getApiInfo(array $params)
    {
        $uid = $params['main_uid'];
        $info = $this->appService->getAppInfoByUid($uid);

        $apiDocList = $this->chApiDocMapper->getApiDocList();

        return [
            'id' => $info['id'],
            'app_id' => $info['app_id'] ?? '',
            'app_secret' => $info['app_secret'] ?? '',
            'notify_switch' => $info['notify_switch'] ?? 0,
            'notify_url' => $info['notify_url'] ?? '',
            'doc_list' => $apiDocList
        ];
    }

    public function saveNotifyConfig(array $data): bool
    {
        $appInfo = SystemApp::query()
            ->where('id',$data['id'])
            ->where('uid',$data['main_uid'])
            ->first();

        if(empty($appInfo)){
            return false;
        }

        $appInfo->notify_switch = $data['notify_switch'];
        if($appInfo->notify_switch){
            $appInfo->notify_url = $data['notify_url'];
        }
        $this->appMapper->clearCache($data['main_uid']);
        return $appInfo->save();
    }

    /**
     * 重置app_id
     * @param array $params
     * @return string
     * @throws \Exception
     */
    public function resetAppId(array $params)
    {
        $uid = $params['main_uid'];
        $info = $this->appService->getAppInfoByUid($uid);
        $appId = $this->appService->getAppId();

        $rst = $this->appMapper->model::where('id',$info['id'])
            ->update(['app_id'=>$appId]);
        if(empty($rst)){
            throw new NormalStatusException('重置失败，请重试');
        }
        $this->appMapper->clearCache($uid);
        return $appId;
    }

    /**
     * 重置app_secret
     * @param array $params
     * @return string
     * @throws \Exception
     */
    public function resetAppSecret(array $params)
    {
        $uid = $params['main_uid'];
        $info = $this->appService->getAppInfoByUid($uid);
        $appSecret = $this->appService->getAppSecret();

        $rst = $this->appMapper->model::where('id',$info['id'])
            ->update(['app_secret'=>$appSecret]);
        if(empty($rst)){
            throw new NormalStatusException('重置失败，请重试');
        }
        $this->appMapper->clearCache($uid);
        return $appSecret;
    }
}